<?php
class Admin_Model_DAO_Subscriber
{    
    private $_db;
    private static $_instance = null;
    
    /**
     * @return Admin_Model_DAO_Subscriber
     * */
    public static function factory() {
    	if(self::$_instance == null) {
    		self::$_instance = new self();
    	}
    
    	return self::$_instance;
    }
    
    private function __construct()
    {
    	$module = Utils_Global::get('module');
    	$this->_db = Utils_Global::getDbInstance($module);
    }
    
   /**
    * Update article by given id and data
    * @param integer $id
    * @param array $data
    * @return true on success, false on failure
    * */
   public function update($id, $data) {
       return $this->_db->update('subscriber', $data, "id = {$this->_db->quote($id, 'INTEGER')}");
   }
   
   /**
    * Delete article by given id
    * @param integer $id
    * @return true on success, false on failure
    * */
   public function delete($id) {
       return $this->_db->delete('subscriber', "id = {$this->_db->quote($id, 'INTEGER')}");
   }
   
   /**
     * Get list of subscriber by given options
     * @param array $options
     * @return array
     * */
	public function select($options = array()) {
		$sql = " SELECT `id`, `full_name`, `email`, `active`, `dateline`,`last_update` ";
		$from = " FROM `subscriber` ";
		$where = " WHERE 1 = 1 ";
		$order = " ORDER BY `active` DESC ";
		if($options['order'] && $options['by']) {
			$order = " ORDER BY `{$options['order']}` {$options['by']} ";
		}
		if($options['active']) {
		    $where .= " AND `active` = 1 ";
		}
		$limit = "";
		if(isset($options['offset']) && $options['offset'] >=0 && $options['limit'] >0) {
			$limit = " limit {$this->_db->quote($options['offset'], 'INTEGER')}, {$this->_db->quote($options['limit'], 'INTEGER')} "; 
		}
		$result = $this->_db->fetchAll ( $sql . $from . $where . $order . $limit );
		return $result;
	}
	
	/**
	 * Get subscriber count by given options
	 * @param array $options
	 * @return integer
	 * */
	public function count($options = array()) {
		$sql = "SELECT count(*) as `count` ";
		$from = " FROM `subscriber` ";
		$where = " WHERE 1 = 1 AND `active` = 1 ";
		$result = $this->_db->fetchOne($sql . $from . $where);
		return $result;
	}
	
	/**
	 * Select newsletter by given condition
	 * @param array $options
	 * @return array
	 * */
	public function selectNewsLetter($options = array()) {
	    $result = array();
	    $sql = " SELECT `id`, `name`, `content`, `dateline`, `last_update` ";
	    $from = " FROM `newsletter` ";
	    $where = " WHERE 1 = 1 ";
	    if(is_array($options)) {
        if($options['id'])
	        $where .= " AND `id` = {$this->_db->quote($options['id'], 'INTEGER')} ";
	    }
	    $limitQuery = "";
	    if(isset($options['offset']) && $options['offset'] >=0 && $options['limit'] > 0) {
	    	$limitQuery = " limit {$this->_db->quote($options['offset'], 'INTEGER')}, {$this->_db->quote($options['limit'], 'INTEGER')} ";
	    }
	    $result = $this->_db->fetchAll($sql . $from . $where . $limitQuery);
//         $result = $sql . $from . $where . $limitQuery;
	    return $result;
	}
	
	/**
	 * Delete newsletter by given condition
	 * @param id
	 * @return boolean
	 * */
	public function deleteNewsLetter($id) {
	    $result = $this->_db->delete('newsletter', "id = {$this->_db->quote($id, 'INTEGER')}");
		return $result;
	}
	
	/**
	 * Insert news letter
	 * @param array $data
	 * @return integer
	 * */
	public function insertNewsLetter($data) {
	    $this->_db->insert('newsletter', $data);
	    $result = $this->_db->lastInsertId('newsletter', 'id');
	    return $result;
	}
	
	/**
	 * Update news letter by given id & data
	 * @param integer $id news letter be updated
	 * @param array $data
	 * @return boolean
	 * */
	public function updateNewsLetter($id, $data) {
	    $result = $this->_db->update('newsletter', $data, "id={$this->_db->quote($id, 'INTEGER')}");
	    return $result;
	}
	
	public function countNewsLetter($options = array()) {
	    $sql = "SELECT count(*) as `count` ";
		$from = " FROM `newsletter` ";
		$where = " WHERE 1 = 1 ";
		$result = $this->_db->fetchOne($sql . $from . $where);
		return $result;
	}
}